package cdc.retrofit.official.annotated;

import java.io.IOException;
import com.google.gson.GsonBuilder;

import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.moshi.MoshiConverterFactory;
import retrofit2.converter.simplexml.SimpleXmlConverterFactory;

@SuppressWarnings("deprecation")
public class AnnotatedConverters {
	public static void main(String... args) throws IOException {
		MockWebServer server = new MockWebServer();
		System.out.println(server.getHostName()+",port="+server.getPort()+"");
		server.enqueue(new MockResponse().setBody("{\"name\": \"Moshi\"}"));
		server.enqueue(new MockResponse().setBody("{\"name\": \"Gson\"}"));
		server.enqueue(new MockResponse().setBody("<user name=\"SimpleXML\"/>"));
		server.enqueue(new MockResponse().setBody("{\"name\": \"Gson\"}"));

		com.squareup.moshi.Moshi moshi = new com.squareup.moshi.Moshi.Builder().build();
		com.google.gson.Gson gson = new GsonBuilder().create();
		MoshiConverterFactory moshiConverterFactory = MoshiConverterFactory.create(moshi);
		GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create(gson);
		@SuppressWarnings("deprecation")
		SimpleXmlConverterFactory simpleXmlConverterFactory = SimpleXmlConverterFactory.create();
		Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/"))
				.addConverterFactory(new AnnotatedConverterFactory.Builder().add(Moshi.class, moshiConverterFactory)
						.add(Gson.class, gsonConverterFactory).add(SimpleXml.class, simpleXmlConverterFactory).build())
				.addConverterFactory(gsonConverterFactory).build();
		Service service = retrofit.create(Service.class);

		Library library1 = service.exampleMoshi().execute().body();
		System.out.println("Library 1: " + library1.name);

		Library library2 = service.exampleGson().execute().body();
		System.out.println("Library 2: " + library2.name);

		Library library3 = service.exampleSimpleXml().execute().body();
		System.out.println("Library 3: " + library3.name);

		Library library4 = service.exampleDefault().execute().body();
		System.out.println("Library 4: " + library4.name);

		server.shutdown();
		
		server.close();
	}

}
